<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>二叉树的遍历 | 数据结构与算法（Python）</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    
    <link rel="prev" href="../chapter7/section1.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="7.2"
        data-chapter-title="二叉树的遍历"
        data-filepath="chapter7/section2.md"
        data-basepath=".."
        data-revision="Fri Mar 31 2017 18:24:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        数据结构与算法（Python）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1/index.html">
            
                
                    <a href="../chapter1/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入概念
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/section1.html">
            
                
                    <a href="../chapter1/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        第一次尝试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/section2.html">
            
                
                    <a href="../chapter1/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        算法的提出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/section3.html">
            
                
                    <a href="../chapter1/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        第二次尝试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/section4.html">
            
                
                    <a href="../chapter1/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        算法效率衡量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/section5.html">
            
                
                    <a href="../chapter1/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        算法分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/section6.html">
            
                
                    <a href="../chapter1/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        常见时间复杂度
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="chapter1/section7.html">
            
                
                    <a href="../chapter1/section7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        Python内置类型性能分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="chapter1/section8.html">
            
                
                    <a href="../chapter1/section8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        数据结构
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/index.html">
            
                
                    <a href="../chapter2/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        顺序表
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/section1.html">
            
                
                    <a href="../chapter2/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        顺序表的形式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/section2.html">
            
                
                    <a href="../chapter2/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        顺序表的结构与实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/section3.html">
            
                
                    <a href="../chapter2/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        顺序表的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="chapter2/section4.html">
            
                
                    <a href="../chapter2/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        Python中的顺序表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/index.html">
            
                
                    <a href="../chapter3/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        链表
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/section1.html">
            
                
                    <a href="../chapter3/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        单向链表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/section2.html">
            
                
                    <a href="../chapter3/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        单项循环链表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/section3.html">
            
                
                    <a href="../chapter3/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        双向链表
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter4/index.html">
            
                
                    <a href="../chapter4/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        栈
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="chapter4/section1.html">
            
                
                    <a href="../chapter4/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        栈结构实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter5/index.html">
            
                
                    <a href="../chapter5/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        队列
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="chapter5/section1.html">
            
                
                    <a href="../chapter5/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        队列的实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="chapter5/section3.html">
            
                
                    <a href="../chapter5/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        双端队列
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter6/index.html">
            
                
                    <a href="../chapter6/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排序与搜索
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="chapter6/section1.html">
            
                
                    <a href="../chapter6/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        冒泡排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="chapter6/section2.html">
            
                
                    <a href="../chapter6/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        选择排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="chapter6/section3.html">
            
                
                    <a href="../chapter6/section3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        插入排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="chapter6/section4.html">
            
                
                    <a href="../chapter6/section4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        快速排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="chapter6/section5.html">
            
                
                    <a href="../chapter6/section5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        希尔排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="chapter6/section6.html">
            
                
                    <a href="../chapter6/section6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        归并排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="chapter6/section7.html">
            
                
                    <a href="../chapter6/section7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        常见排序算法效率比较
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="chapter6/section8.html">
            
                
                    <a href="../chapter6/section8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        搜索
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter7/index.html">
            
                
                    <a href="../chapter7/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        树与树算法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="chapter7/section1.html">
            
                
                    <a href="../chapter7/section1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        二叉树
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="7.2" data-path="chapter7/section2.html">
            
                
                    <a href="../chapter7/section2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        二叉树的遍历
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >数据结构与算法（Python）</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4E8C;&#x53C9;&#x6811;&#x7684;&#x904D;&#x5386;">&#x4E8C;&#x53C9;&#x6811;&#x7684;&#x904D;&#x5386;</h1>
<p>&#x6811;&#x7684;&#x904D;&#x5386;&#x662F;&#x6811;&#x7684;&#x4E00;&#x79CD;&#x91CD;&#x8981;&#x7684;&#x8FD0;&#x7B97;&#x3002;&#x6240;&#x8C13;&#x904D;&#x5386;&#x662F;&#x6307;&#x5BF9;&#x6811;&#x4E2D;&#x6240;&#x6709;&#x7ED3;&#x70B9;&#x7684;&#x4FE1;&#x606F;&#x7684;&#x8BBF;&#x95EE;&#xFF0C;&#x5373;&#x4F9D;&#x6B21;&#x5BF9;&#x6811;&#x4E2D;&#x6BCF;&#x4E2A;&#x7ED3;&#x70B9;&#x8BBF;&#x95EE;&#x4E00;&#x6B21;&#x4E14;&#x4EC5;&#x8BBF;&#x95EE;&#x4E00;&#x6B21;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x8FD9;&#x79CD;&#x5BF9;&#x6240;&#x6709;&#x8282;&#x70B9;&#x7684;&#x8BBF;&#x95EE;&#x79F0;&#x4E3A;&#x904D;&#x5386;&#xFF08;traversal&#xFF09;&#x3002;&#x90A3;&#x4E48;&#x6811;&#x7684;&#x4E24;&#x79CD;&#x91CD;&#x8981;&#x7684;&#x904D;&#x5386;&#x6A21;&#x5F0F;&#x662F;&#x6DF1;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;&#x548C;&#x5E7F;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;,<strong>&#x6DF1;&#x5EA6;&#x4F18;&#x5148;&#x4E00;&#x822C;&#x7528;&#x9012;&#x5F52;&#xFF0C;&#x5E7F;&#x5EA6;&#x4F18;&#x5148;&#x4E00;&#x822C;&#x7528;&#x961F;&#x5217;&#x3002;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x80FD;&#x7528;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#x7684;&#x7B97;&#x6CD5;&#x5927;&#x90E8;&#x5206;&#x4E5F;&#x80FD;&#x7528;&#x5806;&#x6808;&#x6765;&#x5B9E;&#x73B0;&#x3002;</strong></p>
<h2 id="&#x6DF1;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;">&#x6DF1;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;</h2>
<p>&#x5BF9;&#x4E8E;&#x4E00;&#x9897;&#x4E8C;&#x53C9;&#x6811;&#xFF0C;&#x6DF1;&#x5EA6;&#x4F18;&#x5148;&#x641C;&#x7D22;(Depth First Search)&#x662F;&#x6CBF;&#x7740;&#x6811;&#x7684;&#x6DF1;&#x5EA6;&#x904D;&#x5386;&#x6811;&#x7684;&#x8282;&#x70B9;&#xFF0C;&#x5C3D;&#x53EF;&#x80FD;&#x6DF1;&#x7684;&#x641C;&#x7D22;&#x6811;&#x7684;&#x5206;&#x652F;&#x3002;<br>&#x90A3;&#x4E48;&#x6DF1;&#x5EA6;&#x904D;&#x5386;&#x6709;&#x91CD;&#x8981;&#x7684;&#x4E09;&#x79CD;&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#x5E38;&#x88AB;&#x7528;&#x4E8E;&#x8BBF;&#x95EE;&#x6811;&#x7684;&#x8282;&#x70B9;&#xFF0C;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x7684;&#x4E0D;&#x540C;&#x5728;&#x4E8E;&#x8BBF;&#x95EE;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x7684;&#x6B21;&#x5E8F;&#x4E0D;&#x540C;&#x3002;&#x8FD9;&#x4E09;&#x79CD;&#x904D;&#x5386;&#x5206;&#x522B;&#x53EB;&#x505A;&#x5148;&#x5E8F;&#x904D;&#x5386;&#xFF08;preorder&#xFF09;&#xFF0C;&#x4E2D;&#x5E8F;&#x904D;&#x5386;&#xFF08;inorder&#xFF09;&#x548C;&#x540E;&#x5E8F;&#x904D;&#x5386;&#xFF08;postorder&#xFF09;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x7ED9;&#x51FA;&#x5B83;&#x4EEC;&#x7684;&#x8BE6;&#x7EC6;&#x5B9A;&#x4E49;&#xFF0C;&#x7136;&#x540E;&#x4E3E;&#x4F8B;&#x770B;&#x770B;&#x5B83;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x3002;</p>
<ul>
<li>&#x5148;&#x5E8F;&#x904D;&#x5386;
&#x5728;&#x5148;&#x5E8F;&#x904D;&#x5386;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x8BBF;&#x95EE;&#x6839;&#x8282;&#x70B9;&#xFF0C;&#x7136;&#x540E;&#x9012;&#x5F52;&#x4F7F;&#x7528;&#x5148;&#x5E8F;&#x904D;&#x5386;&#x8BBF;&#x95EE;&#x5DE6;&#x5B50;&#x6811;&#xFF0C;&#x518D;&#x9012;&#x5F52;&#x4F7F;&#x7528;&#x5148;&#x5E8F;&#x904D;&#x5386;&#x8BBF;&#x95EE;&#x53F3;&#x5B50;&#x6811;<br>&#x6839;&#x8282;&#x70B9;-&gt;&#x5DE6;&#x5B50;&#x6811;-&gt;&#x53F3;&#x5B50;&#x6811;<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">preorder</span><span class="hljs-params">(self, root)</span>:</span>
      <span class="hljs-string">&quot;&quot;&quot;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#x5148;&#x5E8F;&#x904D;&#x5386;&quot;&quot;&quot;</span>
      <span class="hljs-keyword">if</span> root == <span class="hljs-keyword">None</span>:
          <span class="hljs-keyword">return</span>
      <span class="hljs-keyword">print</span> root.elem
      self.preorder(root.lchild)
      self.preorder(root.rchild)
</code></pre>
</li>
</ul>
<ul>
<li>&#x4E2D;&#x5E8F;&#x904D;&#x5386;
&#x5728;&#x4E2D;&#x5E8F;&#x904D;&#x5386;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x9012;&#x5F52;&#x4F7F;&#x7528;&#x4E2D;&#x5E8F;&#x904D;&#x5386;&#x8BBF;&#x95EE;&#x5DE6;&#x5B50;&#x6811;&#xFF0C;&#x7136;&#x540E;&#x8BBF;&#x95EE;&#x6839;&#x8282;&#x70B9;&#xFF0C;&#x6700;&#x540E;&#x518D;&#x9012;&#x5F52;&#x4F7F;&#x7528;&#x4E2D;&#x5E8F;&#x904D;&#x5386;&#x8BBF;&#x95EE;&#x53F3;&#x5B50;&#x6811;<br>&#x5DE6;&#x5B50;&#x6811;-&gt;&#x6839;&#x8282;&#x70B9;-&gt;&#x53F3;&#x5B50;&#x6811;<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inorder</span><span class="hljs-params">(self, root)</span>:</span>
      <span class="hljs-string">&quot;&quot;&quot;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#x4E2D;&#x5E8F;&#x904D;&#x5386;&quot;&quot;&quot;</span>
      <span class="hljs-keyword">if</span> root == <span class="hljs-keyword">None</span>:
          <span class="hljs-keyword">return</span>
      self.inorder(root.lchild)
      <span class="hljs-keyword">print</span> root.elem
      self.inorder(root.rchild)
</code></pre>
</li>
</ul>
<ul>
<li>&#x540E;&#x5E8F;&#x904D;&#x5386;
&#x5728;&#x540E;&#x5E8F;&#x904D;&#x5386;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x9012;&#x5F52;&#x4F7F;&#x7528;&#x540E;&#x5E8F;&#x904D;&#x5386;&#x8BBF;&#x95EE;&#x5DE6;&#x5B50;&#x6811;&#x548C;&#x53F3;&#x5B50;&#x6811;&#xFF0C;&#x6700;&#x540E;&#x8BBF;&#x95EE;&#x6839;&#x8282;&#x70B9;<br>&#x5DE6;&#x5B50;&#x6811;-&gt;&#x53F3;&#x5B50;&#x6811;-&gt;&#x6839;&#x8282;&#x70B9;<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">postorder</span><span class="hljs-params">(self, root)</span>:</span>
      <span class="hljs-string">&quot;&quot;&quot;&#x9012;&#x5F52;&#x5B9E;&#x73B0;&#x540E;&#x7EED;&#x904D;&#x5386;&quot;&quot;&quot;</span>
      <span class="hljs-keyword">if</span> root == <span class="hljs-keyword">None</span>:
          <span class="hljs-keyword">return</span>
      self.postorder(root.lchild)
      self.postorder(root.rchild)
      <span class="hljs-keyword">print</span> root.elem
</code></pre>
<img src="../images/&#x4E09;&#x79CD;&#x904D;&#x5386;&#x7ED3;&#x679C;.jpg" alt="&#x4E09;&#x79CD;&#x904D;&#x5386;&#x7ED3;&#x679C;"></li>
</ul>
<blockquote>
<p>&#x8BFE;&#x5802;&#x7EC3;&#x4E60;&#xFF1A;
&#x6309;&#x7167;&#x5982;&#x56FE;&#x6811;&#x7684;&#x7ED3;&#x6784;&#x5199;&#x51FA;&#x4E09;&#x79CD;&#x904D;&#x5386;&#x7684;&#x987A;&#x5E8F;:<br><img src="../images/&#x6811;&#x7EC3;&#x4E60;.png" alt="&#x6811;&#x7EC3;&#x4E60;"><br>&#x7ED3;&#x679C;&#xFF1A;<br>&#x5148;&#x5E8F;:a b c  d e f g h<br>&#x4E2D;&#x5E8F;:b d c e a  f h g<br>&#x540E;&#x5E8F;:d e c b h g f a<br><strong>&#x601D;&#x8003;&#xFF1A;</strong>&#x54EA;&#x4E24;&#x79CD;&#x904D;&#x5386;&#x65B9;&#x5F0F;&#x80FD;&#x591F;&#x552F;&#x4E00;&#x7684;&#x786E;&#x5B9A;&#x4E00;&#x9897;&#x6811;&#xFF1F;&#xFF1F;&#xFF1F;</p>
</blockquote>
<h2 id="&#x5E7F;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;&#x5C42;&#x6B21;&#x904D;&#x5386;">&#x5E7F;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;(&#x5C42;&#x6B21;&#x904D;&#x5386;)</h2>
<p>&#x4ECE;&#x6811;&#x7684;root&#x5F00;&#x59CB;&#xFF0C;&#x4ECE;&#x4E0A;&#x5230;&#x4E0B;&#x4ECE;&#x4ECE;&#x5DE6;&#x5230;&#x53F3;&#x904D;&#x5386;&#x6574;&#x4E2A;&#x6811;&#x7684;&#x8282;&#x70B9;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">breadth_travel</span><span class="hljs-params">(self, root)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x5229;&#x7528;&#x961F;&#x5217;&#x5B9E;&#x73B0;&#x6811;&#x7684;&#x5C42;&#x6B21;&#x904D;&#x5386;&quot;&quot;&quot;</span>
        <span class="hljs-keyword">if</span> root == <span class="hljs-keyword">None</span>:
            <span class="hljs-keyword">return</span>
        queue = []
        queue.append(root)
        <span class="hljs-keyword">while</span> queue:
            node = queue.pop(<span class="hljs-number">0</span>)
            <span class="hljs-keyword">print</span> node.elem,
            <span class="hljs-keyword">if</span> node.lchild != <span class="hljs-keyword">None</span>:
                queue.append(node.lchild)
            <span class="hljs-keyword">if</span> node.rchild != <span class="hljs-keyword">None</span>:
                queue.append(node.rchild)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter7/section1.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 二叉树"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
